﻿CREATE PROCEDURE [dbo].[proc_Order_Item_Delete]
	(
		@Id int
	)
AS
	Delete From Order_Item Where Id=@Id AND Occupancy IS NOT NULL
	if @@ROWCOUNT=0
	BEGIN
	DECLARE @stateId INT
	SET @stateId=-100
		SELECT @stateId= StateId FROM [Order] WHERE oid IN (SELECT Order_Item.OId
		                                                      FROM Order_Item WHERE id=@id)
	IF @stateId<0
		Delete From Order_Item Where Id=@Id
	ELSE
		Delete From Order_Item Where Id=-100
	END
	RETURN
